package org.example.demo4.utils.login;
import java.util.Collections;

import org.example.demo4.service.impl.UserDetailsServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Component;
 
@Component
public class ExtsAuthenticationProvider   implements AuthenticationProvider {
    @Autowired
    private UserDetailsServiceImpl userDetailsService;
 
    /**
     * 认证逻辑
     */
    @Override
    public Authentication authenticate(Authentication authentication) throws AuthenticationException {
        ExtsAuthenticationToken ExtsAuthenticationToken = (ExtsAuthenticationToken) authentication;
 
        String username = (String) ExtsAuthenticationToken.getPrincipal();
        UserDetails user = userDetailsService.loadUserByUsername(username);
        ExtsAuthenticationToken result = new ExtsAuthenticationToken(user, Collections.emptyList());
        /*
        Details 中包含了 ip地址、 sessionId 等等属性 也可以存储一些自己想要放进去的内容
        */
        result.setDetails(ExtsAuthenticationToken.getDetails());
        return result;
    }
 
    /**
     *UserIdAuthenticationToken交给UserIdAuthenticationProvider处理
     * @param aClass
     * @return
     */
    @Override
    public boolean supports(Class<?> aClass) {
        return ExtsAuthenticationToken.class.isAssignableFrom(aClass);
 
    }
}